/********************************************************************************************************
 * @file OtaSettingService.java
 *
 * @brief for TLSR chips
 *
 * @author telink
 * @date Sep. 30, 2019
 *
 * @par Copyright (c) 2019, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *
 *          Licensed under the Apache License, Version 2.0 (the "License");
 *          you may not use this file except in compliance with the License.
 *          You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 *          Unless required by applicable law or agreed to in writing, software
 *          distributed under the License is distributed on an "AS IS" BASIS,
 *          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *          See the License for the specific language governing permissions and
 *          limitations under the License.
 *******************************************************************************************************/
package com.telink.lt;

import android.content.Context;

import com.telink.ota.foundation.OtaSetting;
import com.telink.ota.util.FileSystem;

public class OtaSettingService {
    public static final String CACHE_NAME = "com.telink.ota.settings";

    private static OtaSettingService instance = new OtaSettingService();

    public static OtaSettingService getInstance() {
        return instance;
    }

    private OtaSettingService() {
    }

    public OtaSetting otaSetting;

    public OtaSetting get() {
        return otaSetting;
    }

    public void load(Context context) {
        otaSetting = readInCache(context);
        if (otaSetting == null) {
            otaSetting = createDefault();
        }
    }

    public OtaSetting createDefault() {
        return new OtaSetting();
    }

    public void updateSettings(Context context, OtaSetting setting) {
        otaSetting = setting;
        FileSystem.writeAsObject(context, CACHE_NAME, setting);
    }

    public OtaSetting readInCache(Context context) {
        Object object = FileSystem.readAsObject(context, CACHE_NAME);
        if (object != null) {
            return (OtaSetting) object;
        }
        return null;
    }
}
